package com.cloudkinto.controller.stock;


import com.cloudkinto.base.BaseController;
import com.cloudkinto.bo.stock.*;
import com.cloudkinto.common.annotations.UserSessionRequired;
import com.cloudkinto.common.utils.dict.StaticDict;
import com.cloudkinto.common.web.PageResult;
import com.cloudkinto.common.web.SingleResult;
import com.cloudkinto.dto.stock.adjust.box.BoxCellStockDto;
import com.cloudkinto.dto.stock.adjust.sku.SkuCellStockDto;
import com.cloudkinto.dto.stock.flowing.FlowingPageDto;
import com.cloudkinto.dto.stock.query.BoxCellStockPageDto;
import com.cloudkinto.dto.stock.query.BoxTypeStockPageDto;
import com.cloudkinto.dto.stock.query.SkuCellStockPageDto;
import com.cloudkinto.dto.stock.query.SkuStockPageDto;
import com.cloudkinto.service.stock.ProductStockService;
import com.cloudkinto.vo.sc.faraway.FarawayRuleImportRes;
import com.cloudkinto.vo.stock.StockLockingVo;
import com.cloudkinto.vo.stock.adjust.box.AdjustItemRes;
import com.cloudkinto.vo.stock.adjust.box.BoxAdjustInitReq;
import com.cloudkinto.vo.stock.adjust.box.BoxAdjustReq;
import com.cloudkinto.vo.stock.adjust.box.BoxConvertReq;
import com.cloudkinto.vo.stock.adjust.sku.SkuAdjustInitReq;
import com.cloudkinto.vo.stock.adjust.sku.SkuAdjustReq;
import com.cloudkinto.vo.stock.move.BoxMoveReq;
import com.cloudkinto.vo.stock.move.sku.SkuMoveReq;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author hua
 * @since 2024-10-28
 */
@RestController
@RequiredArgsConstructor
@Api(tags = "wms库存模块")
@RequestMapping("/api/admin/wms/ProductStock")
public class ProductStockWmsController extends BaseController {
    private final ProductStockService productStockService;

    @UserSessionRequired
    @GetMapping("/pageInit")
    @ApiOperation(value = "查询初始化", notes = "查询初始化")
    public SingleResult pageInit() {
        return productStockService.pageInit(getCurrentUserId(), getTenantId(), getCompanyId(), "wms");
    }

    @UserSessionRequired
    @PostMapping("/productListPageBySku")
    @ApiOperation(value = "按产品查询", notes = "按产品查询")
    @ApiResponses(@ApiResponse(code = 200, message = "", response = SkuStockPageDto.class))
    public PageResult productListPageBySku(@RequestBody @Validated ProductStockBySkuQueryBo req) {
        req.setSource("wms");
        req.setTenantId(getTenantId());
        req.setUserId(getCurrentUserId());
        req.setWhIdList(Collections.singletonList(getWarehouseId()));
        return productStockService.productListPageBySku(req);
    }

    @UserSessionRequired
    @PostMapping("getProductLockingStockDetail")
    @ApiOperation(value = "产品锁定库存查询", notes = "产品锁定库存查询")
    @ApiResponses(@ApiResponse(code = 200, message = "", response = StockLockingVo.class))
    public PageResult getProductLockingStockDetail(@RequestBody @Validated ProductStockLockingQueryBo req) {
        req.setWhId(getWarehouseId());
        return productStockService.getProductLockingStockDetail(req);
    }

    @UserSessionRequired
    @PostMapping("/productListPageByLocation")
    @ApiOperation(value = "产品-按库位查询", notes = "按库位查询")
    @ApiResponses(@ApiResponse(code = 200, message = "", response = SkuCellStockPageDto.class))
    public PageResult productListPageByLocation(@RequestBody @Validated ProductStockByLocationQueryBo req) {
        req.setTenantId(getTenantId());
        req.setWhId(getWarehouseId());
        return productStockService.productListPageByLocation(req);
    }

    @UserSessionRequired
    @PostMapping("/boxListPageByType")
    @ApiOperation(value = "按箱类型查询", notes = "按箱类型查询")
    @ApiResponses(@ApiResponse(code = 200, message = "", response = BoxTypeStockPageDto.class))
    public PageResult boxListPageByType(@RequestBody @Validated BoxStockByTypeQueryBo req) {
        req.setWhIdList(Collections.singletonList(getWarehouseId()));
        req.setSource("wms");
        req.setTenantId(getTenantId());
        req.setUserId(getCurrentUserId());
        return productStockService.boxListPageByType(req);
    }

    @UserSessionRequired
    @PostMapping("getBoxLockingStockDetail")
    @ApiOperation(value = "箱锁定库存查询", notes = "箱锁定库存查询")
    @ApiResponses(@ApiResponse(code = 200, message = "", response = StockLockingVo.class))
    public PageResult getBoxLockingStockDetail(@RequestBody @Validated BoxStockLockingQueryBo req) {
        req.setWhId(getWarehouseId());
        return productStockService.getBoxLockingStockDetail(req);
    }

    @UserSessionRequired
    @PostMapping("/boxListPageByLocation")
    @ApiOperation(value = "箱子-按库位查询", notes = "箱子-按库位查询")
    @ApiResponses(@ApiResponse(code = 200, message = "", response = BoxCellStockPageDto.class))
    public PageResult productListPageByLocation(@RequestBody @Validated BoxStockByLocationQueryBo req) {
        req.setWhId(getWarehouseId());
        req.setSource("wms");
        req.setTenantId(getTenantId());
        req.setUserId(getCurrentUserId());
        return productStockService.boxListPageByLocation(req);
    }

    @UserSessionRequired
    @PostMapping("/boxCellStockList")
    @ApiOperation(value = "箱子-库存调整/移库位初始化", notes = "箱子-库存调整/移库位初始化")
    @ApiResponses(@ApiResponse(code = 200, message = "", response = BoxCellStockDto.class))
    public SingleResult boxCellStockList(@RequestBody @Validated BoxAdjustInitReq req) {
        List<BoxCellStockDto> skuCellList = productStockService.boxCellStockList(req, getWarehouseId(), getTenantId("wms"));
        return SingleResult.success(skuCellList);
    }


    @UserSessionRequired
    @GetMapping("/getBoxListForAdjust")
    @ApiOperation(value = "箱子-库存调整-添加箱子（分页）", notes = "箱子-库存调整-添加箱子（分页）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "companyIds", value = "客户 多个,隔开"),
            @ApiImplicitParam(name = "keyword", value = "关键词"),
            @ApiImplicitParam(name = "current", value = "当前页"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小")
    })
    @ApiResponses(@ApiResponse(code = 200, message = "", response = FlowingPageDto.class))
    public PageResult getBoxListForAdjust(HttpServletRequest request) {

        Map<String, Object> map = new HashMap<>();
        map.put("tenantId", getTenantId(StaticDict.System_Source.WMS.getValue()));
        map.put("source", "wms");
        map.put("warehouseIds", Collections.singletonList(getWarehouseId()));
        String companyIds = request.getParameter("companyIds");
        if (StringUtils.isNotBlank(companyIds)) {
            map.put("companyIds", Arrays.asList(companyIds.split(",")));
        }
        String keyword = request.getParameter("keyword");
        map.put("keyword", keyword);
        String current = request.getParameter("current");
        String size = request.getParameter("pageSize");
        map.put("current", current);
        map.put("pageSize", size);
        return productStockService.getBoxListForAdjust(map);
    }


    @UserSessionRequired
    @PostMapping("/skuCellStockList")
    @ApiOperation(value = "产品库存-库存调整/移库位/转换初始化", notes = "箱子-库存调整/移库位初始化")
    @ApiResponses(@ApiResponse(code = 200, message = "", response = SkuCellStockDto.class))
    public SingleResult skuCellStockList(@RequestBody @Validated SkuAdjustInitReq req) {
        List<SkuCellStockDto> skuCellList = productStockService.skuCellStockList(req, getWarehouseId(), getTenantId("wms"));
        return SingleResult.success(skuCellList);
    }


    @UserSessionRequired
    @PostMapping("/boxStockAdjust")
    @ApiOperation(value = "箱子-库存调整", notes = "箱子-库存调整")
    @ApiResponses(@ApiResponse(code = 200, message = "", response = AdjustItemRes.class))
    public SingleResult boxStockAdjust(@RequestBody @Validated BoxAdjustReq req) {
        List<AdjustItemRes> adjustItemRes = productStockService.boxStockAdjust(req, getWarehouseId(), getCurrentUserId(), getTenantId("wms"));
        return SingleResult.success(adjustItemRes);
    }


    @UserSessionRequired
    @PostMapping("/boxMoveStock")
    @ApiOperation(value = "箱子-移库", notes = "箱子-移库")
    @ApiResponses(@ApiResponse(code = 200, message = "", response = AdjustItemRes.class))
    public SingleResult boxMoveStock(@RequestBody @Validated BoxMoveReq req) {
        List<AdjustItemRes> adjustItemRes = productStockService.boxMoveStock(req, getWarehouseId(), getCurrentUserId(), getTenantId("wms"));
        return SingleResult.success(adjustItemRes);
    }


    @UserSessionRequired
    @PostMapping("/boxConvert")
    @ApiOperation(value = "箱子-转换库存", notes = "箱子-转换库存")
    @ApiResponses(@ApiResponse(code = 200, message = "", response = BoxConvertReq.class))
    public SingleResult boxConvert(@RequestBody @Validated BoxConvertReq req) {
        List<AdjustItemRes> adjustItemRes = productStockService.boxConvert(req, getWarehouseId(), getTenantId("wms"));
        return SingleResult.success(adjustItemRes);
    }

    @UserSessionRequired
    @PostMapping("/skuStockAdjust")
    @ApiOperation(value = "产品库存-库存调整", notes = "箱子-库存调整")
    @ApiResponses(@ApiResponse(code = 200, message = "", response = AdjustItemRes.class))
    public SingleResult skuStockAdjust(@RequestBody @Validated SkuAdjustReq req) {
        List<AdjustItemRes> adjustItemRes = productStockService.skuStockAdjust(req, getWarehouseId(), getCurrentUserId(), getTenantId("wms"));
        return SingleResult.success(adjustItemRes);
    }


    @UserSessionRequired
    @PostMapping("/skuMoveStock")
    @ApiOperation(value = "产品库存-移库", notes = "箱子-移库")
    @ApiResponses(@ApiResponse(code = 200, message = "", response = AdjustItemRes.class))
    public SingleResult skuMoveStock(@RequestBody @Validated SkuMoveReq req) {
        List<AdjustItemRes> adjustItemRes = productStockService.skuMoveStock(req, getWarehouseId(), getCurrentUserId(), getTenantId("wms"));
        return SingleResult.success(adjustItemRes);
    }

    @UserSessionRequired
    @PostMapping("/importSkuForAdjust")
    @ApiOperation(value = "库存调整 导入sku")
    @ApiResponses(@ApiResponse(code = 200, message = "", response = SkuCellStockDto.class))
    public SingleResult importSkuForAdjust(MultipartFile file, HttpServletRequest request) {
        String companyId = request.getParameter("companyId");
        Map<String, Object> map = productStockService.importSkuForAdjust(file, StringUtils.isBlank(companyId) ? null : Long.parseLong(companyId));
        return SingleResult.success(map);
    }

}